<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
   <!-- 搜索信息 -->
    <view class="sugg_list" v-if="searchResult.length != 0">
      <navigator class="sugg_item" v-for="(item,index) in searchResult " :key="item.goods_id"
      :url="'/subPackage/goods_detail/goods_detail?goods_id= ' + item.goods_id">
        <view class="goods_name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </navigator>
    </view>
    
    <!-- 搜索历史 -->
    
    <view class="history-box"  v-else="searchResult.length == 0">
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="18" @click="clearHistory"></uni-icons>
      </view>
     <uni-tag class="history-list-tag" v-for="(item,index) in historys" :text="item" :key="index"
      customStyle="margin-top: 5px; margin-right: 10px; margin-left:10px; display:inline-block;"
      @click="gotoGoodsList(item)">
      </uni-tag>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        kw:'',
        searchResult:[],
        historyList:['李白','字帖','手机']
      };
     
    },
    	methods: {
    			input(e){
            clearTimeout(this.timer)
            this.timer = setTimeout(()=>{
              console.log(e);
              this.kw = e
              this. getSearchList();
              console.log('------');
            },1000)
    				
    			},
          async getSearchList(){
           
            if(this.kw == ''){
              this.searchResult = []
              return
            }
             let {data:result} = await uni.$http.get('/api/public/v1/goods/qsearch',{query: this.kw});
             if(result.meta.status != 200){
               console.log(result);
               uni.$showMsg();
               return
             }
             this.searchResult = result.message
             this.saveSearchHistory()
             console.log(this.searchResult);
          },
         	saveSearchHistory(){
         				// 直接把关键词push到数组中
         				// this.historyList.push(this.kw);
         				
         				// 1、将数组Array转换为Set对象，快速去重
         				let set = new Set(this.historyList);
         				
         				// 2、调用 Set 对象的 delete 方法，删除对应的元素
         				set.delete(this.kw);
         				
         				// 3、调用 Set 对象的 add 方法，添加元素
         				set.add(this.kw);
         				
         				// 4、将 Set 对象转换为 Array 数组
         				this.historyList = Array.from(set);
                uni.setStorageSync('kw',JSON.stringify(this.historyList));
         			},
              clearHistory(){
                this.historyList = [];
                				// 2、清空本地存储的搜索历史
                			uni.setStorageSync('kw','[]');
              },
              gotoGoodsList(kw){
            
                uni.navigateTo({
                url: '/subPackage/good_list/good_list?query=' + kw
                })
              }
    		},
     	computed: {
     			historys() {
     				return [...this.historyList].reverse();
     			}
     		},
        onLoad(){
          this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
        }
        
  }
</script>

<style lang="scss" scoped>
.uni-searchbar {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	flex-direction: row;
	position: relative;
	padding: 16rpx;
    /* 将默认的 #FFFFFF 改为 #C00000 */
	background-color: #C00000;
}
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
    background-color: aquamarine;
	}
  .sugg_list{
    padding: 0 5px;
    .sugg_item{
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 13px 0;
      border-bottom: 1px solid salmon;
      font-size: 12px;
      .goods_name{
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
    
  .history-title{
    display: flex;
    justify-content: space-between;
    height: 40px;
    align-items: center;
    font-size: 13px;
    border-bottom:1px   solid  deeppink ;
    
 
  }
</style>
